package com.wwm.makerandroid.makerandroid;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lipiao.makerandroid.R;


import com.wwm.makerandroid.makerandroid.Utils.DateUtil;
import com.wwm.makerandroid.makerandroid.Utils.Divisions;
import com.wwm.makerandroid.makerandroid.Utils.HttpUtil;
import com.wwm.makerandroid.makerandroid.Utils.SqliteUtils;
import com.wwm.makerandroid.makerandroid.View.Fragment.ApplyFragment;
import com.wwm.makerandroid.makerandroid.View.Fragment.ClearanceFragment;
import com.wwm.makerandroid.makerandroid.View.Fragment.RecordFragment;
import com.wwm.makerandroid.makerandroid.View.Fragment.ShenheFragment;
import com.wwm.makerandroid.makerandroid.View.Fragment.TongjiFragment;
import com.ycl.tabview.library.TabView;
import com.ycl.tabview.library.TabViewChild;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
    static String TAG = "MainActivity";

    static String userNumber;//账号
    static String password;//密码
    String role;
    @BindView(R.id.mainactivity)
    View rootView;
//    姓名
    String name="姓名";
    TextView nameText;
//    头像
    ImageView headImage;
    @BindView(R.id.tabView)
    TabView tabView;
    List<TabViewChild> tabViewChildList = new ArrayList<>();
    private long lastClickBackTime = System.currentTimeMillis() - 3000;//三秒间隔 间隔内两次返回提示用户是否退出

    private ApplyFragment applyFragment;//首页

    private ShenheFragment shenheFragment;//审核
    private ClearanceFragment clearanceFragment;//销假
    private RecordFragment recordFragment;//记录
    public String areaJsonStr;

//    统计页面
    private TongjiFragment tongjiFragment;//记录

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //绑定 初始化 ButterKnife
        ButterKnife.bind(this);
        //初始化 全局网络请求工具类 时间戳格式化工具类  SQLite数据库工具类 均为单例
        HttpUtil.getInstance();
        DateUtil.getInstance();
        SqliteUtils.getInstance(getBaseContext());

        //接受LoginActivity传来的数据
        Intent intent = getIntent();
        userNumber = intent.getStringExtra("userNumber");
        password = intent.getStringExtra("password");
        role = intent.getStringExtra("role");
        name=intent.getStringExtra("name");
        headImage=rootView.findViewById(R.id.tv_role);
//        头像的图片地址
        int imageUrl=R.drawable.student;
        switch (role){
            case  "1":
                imageUrl=R.drawable.student;
                break;
            case  "2":
                imageUrl=R.drawable.teacher;
                break;
            case  "3":
                imageUrl=R.drawable.shuji;
                break;
            case  "4":
                imageUrl=R.drawable.renshi;
                break;
                case  "5":
                imageUrl=R.drawable.admin;
                break;
            default:
                imageUrl=R.drawable.student;

        }
        headImage.setImageResource(imageUrl);
        nameText=rootView.findViewById(R.id.text_name);
        nameText.setText(name);

        Log.d(TAG, "onCreate: userNumber " + userNumber + " password " + password);

        //先判空后添加
//        if (mainFragment == null) {
//            mainFragment = MainFragment.newInstance(userNumber);
//            tabViewChildList.add(new TabViewChild(R.mipmap.main, R.mipmap.main_no, "请假", mainFragment));
//        }
        if (applyFragment == null) {
            ApplyFragment applyFragment = new ApplyFragment();
            applyFragment.newInstance(userNumber, this);
            tabViewChildList.add(new TabViewChild(R.mipmap.main, R.mipmap.main_no, "请假", applyFragment));
        }
        if (clearanceFragment == null) {
            clearanceFragment = ClearanceFragment.newInstance( userNumber);
            tabViewChildList.add(new TabViewChild(R.mipmap.lead, R.mipmap.lead_no, "销假", clearanceFragment));
        }
        if (shenheFragment == null) {
            shenheFragment = ShenheFragment.newInstance( userNumber);
            tabViewChildList.add(new TabViewChild(R.mipmap.project, R.mipmap.user_no, "审批", shenheFragment));
        }
        if (recordFragment == null) {
            recordFragment = RecordFragment.newInstance(userNumber);
            tabViewChildList.add(new TabViewChild(R.mipmap.user, R.mipmap.project_no, "记录", recordFragment));
        }


//        如果是级别是老师及以上的的话可以查看记录
        if(!"1".equals(role)){
            if (tongjiFragment == null) {
                tongjiFragment = TongjiFragment.newInstance(userNumber,this);
                tabViewChildList.add(new TabViewChild(R.mipmap.user, R.mipmap.project_no, "统计", tongjiFragment));
            }
        }

        //getSupportFragmentManager() 需要基类继承自AppCompatActivity
        tabView.setTabViewChild(tabViewChildList, getSupportFragmentManager());



        //     todo   读取文件会占用很长时间单独开一个线程执行  执行完了之后给Tongji Apply 页面 地区选择器赋值
        Reader reader = null;
        StringBuffer sb=new StringBuffer();
        //        读取json格式 的地区文件  https://blog.csdn.net/m0_46357847/article/details/126656710
        try {
            InputStream inputStream = this.getResources().openRawResource(R.raw.area);
            reader = new InputStreamReader(inputStream);
            int ch = 0;
            char[] buffer = new char[1024];
            sb = new StringBuffer();
            while ((ch = reader.read(buffer)) != -1) {
                sb.append(buffer, 0, ch);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
      Divisions.get(sb.toString());
    }

    //退出提醒 两次back退出
    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - lastClickBackTime > 2000) { // 后退阻断
            Toast.makeText(this, "再点一次退出", Toast.LENGTH_LONG).show();
            lastClickBackTime = System.currentTimeMillis();
        } else { // 关掉app
            //System.exit(0);//完全退出  再次启动很慢
            finish();//保留进程 再次启动较快
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
